Public Class formstore

    ' Tells if the application can be exited.
    Private m_bAllowClose As Boolean = False

    ' The vault is stored here.
    Private m_oVault As MFilesAPI.Vault = Nothing

    ' Scanned image.
    Private m_szScannedImagePath As String = ""

    ' Tells if the back button is clicked.
    Private m_bBackButtonClicked As Boolean = False

    ' Tells if the Attach button is clicked.
    Private m_bAttachButtonClicked As Boolean = False

    ' Initialize the form.
    Public Sub Initialize(ByVal oVault As MFilesAPI.Vault, ByVal szScannedImagePath As String)
        m_oVault = oVault
        m_szScannedImagePath = szScannedImagePath
    End Sub

    ' Tells if the back button is clicked.
    Function IsBackButtonClicked() As Boolean
        Return m_bBackButtonClicked
    End Function

    ' Tells if the attach button is clicked.
    Function IsAttachButtonClicked() As Boolean
        Return m_bAttachButtonClicked
    End Function

    ' Fires when the dialog is loaded.
    Private Sub formstore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    ' Fires when the form is closing.
    Private Sub formstore_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' Prevent closing if not allowed.
        If Not m_bAllowClose Then
            e.Cancel = True
        End If

    End Sub

    ' Create new document button clicked.
    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

        Try

            ' Show document card.
            Dim oObjCreationInfo As New MFilesAPI.ObjectCreationInfo
            oObjCreationInfo.SetObjectType(0, False)  ' Document
            Dim oSourceFiles As New MFilesAPI.SourceObjectFiles
            oSourceFiles.AddFile("Scanned", "pdf", m_szScannedImagePath)
            oObjCreationInfo.SetSourceFiles(oSourceFiles)
            oObjCreationInfo.SetDisableObjectCreation(False)
            oObjCreationInfo.SetSingleFileDocument(False, False) ' Must be MFD, because a bug in API.
            Dim oObjWinResult As MFilesAPI.ObjectWindowResult
            oObjWinResult = m_oVault.ObjectOperations.ShowPrefilledNewObjectWindow(Me.Handle, MFilesAPI.MFObjectWindowMode.MFObjectWindowModeInsertSourceFiles, oObjCreationInfo)

            ' Check if the creation of an document was cancelled.
            If (oObjWinResult.Result = MFilesAPI.MFObjectWindowResultCode.MFObjectWindowResultCodeCancel) Then
                Throw New System.Exception("Document creation cancelled by the user.")
            End If

            ' Convert the created MFD to SFD.
            m_oVault.ObjectOperations.SetSingleFileObject(oObjWinResult.ObjVer, True)

            ' Check the document in.
            m_oVault.ObjectOperations.CheckIn(oObjWinResult.ObjVer)

            ' Close the dialog.
            m_bAllowClose = True
            Me.Close()

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

    End Sub

    ' Back button clicked.
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click

        Try

            ' Log out for security reasons.
            If (IsNothing(m_oVault) = False) Then
                m_oVault.LogOutWithDialogs(Me.Handle)
            End If

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Close the dialog.
        m_bBackButtonClicked = True
        m_bAllowClose = True
        Me.Close()

    End Sub

    ' Attach button clicked.
    Private Sub btnAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttach.Click

        ' Close the dialog.
        m_bAttachButtonClicked = True
        m_bAllowClose = True
        Me.Close()

    End Sub

End Class